package com.wf.smartcomm.server.weixin.action;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;

import com.alibaba.druid.sql.visitor.functions.Concat;
import com.opensymphony.xwork2.ModelDriven;
import com.wf.smartcomm.server.bean.Merchant;
import com.wf.smartcomm.server.bean.User;
import com.wf.smartcomm.server.client.action.BaseAction;
import com.wf.smartcomm.server.service.MerchantService;
import com.wf.smartcomm.server.service.UserService;
import com.wf.smartcomm.server.util.PushUtils;
import com.wf.smartcomm.server.weixin.util.WeixinUtil;

import net.sf.json.JSONObject;

public class WeiChatMerchantAction  extends BaseAction{
	
	
	
	
	@Resource
	private MerchantService merchantService;
	
	@Resource
	private UserService userService;
	

	/**
	 * 判断是否登录
	 * @return
	 * @throws Exception
	 */
	public String list() throws Exception {
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		if (user != null) {
			return "success";
		}else {
//			Cookie[] cookies = request.getCookies();
//			if (cookies != null && cookies.length > 0) {
//				for(Cookie cookie : cookies){
//		            if(cookie.getName().equals("userId")){
//		            	String userId = cookie.getValue();
//		            	user = userService.getUserById(userId);
//		    			session.setAttribute("user",user);
//		    			return "success";
//		            }
//		        }
//			}
			String code = request.getParameter("code");
			logger.info("code--------->"+code);
			String oauth_full_url ="";
			if (PushUtils.isDevEnv) {
				oauth_full_url=oauth_base_url_dev+"&code="+code+"&grant_type=authorization_code";
			}else {
				oauth_full_url=oauth_base_url_gen+"&code="+code+"&grant_type=authorization_code";
			}
			JSONObject jsonObject = WeixinUtil.httpRequest(oauth_full_url, "GET", null);
//			if (jsonObject != null) {
				logger.info(jsonObject);
				String openId = jsonObject.getString("openid");
				logger.info("openId-------->:"+openId);
				user = userService.getUserByOpenId(openId);
//				if (user == null) {
//					Cookie cookie = new Cookie("openId", openId);
//					cookie.setMaxAge(3600*24*30*12*2);
//					response.addCookie(cookie);
//					return "login";
//				}
//				Cookie cookie = new Cookie("userId", user.getObjectId());
//				cookie.setMaxAge(3600*24*30*12*2);
//				response.addCookie(cookie);
				session.setAttribute("user", user);
				return "success";
				
//			}else {
//				logger.info("jsonObject is null");
//				return "login";
//			}
		}
	}
	
	/**
	 * 上拉刷新，下拉加载
	 * @throws Exception
	 */
	public void more()throws Exception{
		int type=0;
		if(request.getParameter("type")!=null){
			type=Integer.parseInt(request.getParameter("type"));
		}
		page = merchantService.pageList(page,type);
		printJsonData(gson.toJson(page));
	}
	
	
	/**
	 * 根据积分商家的类型查询积分商家
	 * @throws Exception
	 */
	/*public void getMerchantByType()throws Exception{
		int type=Integer.parseInt(request.getParameter("type"));
		if(type==0){
			page = merchantService.pageList(page);
		}
		else{
			page=merchantService.getMerchantByType(page, type);
		}
		page = merchantService.pageList(page,type);
		printJsonData(gson.toJson(page));
	}*/
}
